$(function(){
    loginName=baseFunction.getUrlParameterValueByKey("loginName");
    channelCode=baseFunction.getUrlParameterValueByKey("channelCode"); //通道编码
    id=baseFunction.getUrlParameterValueByKey("id"); //通道ID
    merchno=baseFunction.getUrlParameterValueByKey("merchno"); //商务号
    accountBalance=""; //账户余额
    getchannelData(); //获取通道列表数据
    getList();
    getAisleInfo();

    //添加信用卡
    $("#addBankCard").on("click",function(){
      window.location.href="my_addBankCard.html?loginName="+loginName+"";
    })
    // 解绑信用卡
    $(".bankList").on("click","#untieTheCard",function(){
      $(".mask .tip").html("您确定要解绑此卡吗?");
      $(".mask").show(0);
      var cardno=$(this).attr("data_cardno");
      var btn=document.querySelector(".mask .btn");
      var mask=document.querySelector(".mask");
      btn.onclick=function(){
        removeBankCard(cardno);
      }
    })
    // 修改银行卡详情
    $(".bankList").on("click","#gotoModifyCreditCard",function(){
      var id=$(this).attr("data_id");
      window.location.href="my_modifyCreditCard.html?loginName="+loginName+"&id="+id+"";
    }) 
})

/**
 * 获取通道列表 以获取基本数据
 */
function getchannelData(){
  var data={
      userName: baseFunction.encryption(loginName),
      queryType:"6001",
      arg1: baseFunction.encryption(merchno),
  };
  baseFunction.netRequest("param",data,function(res){
      
      var dataList=JSON.parse(JSON.parse(res.data).result);
      dataList.forEach(function(key){
          if(id==key.id){
            console.log(key);
            addRate=key.addRate; //额外增加费率
            ruleCode=key.ruleCode;
            rmentMode=key.rmentMode;
            channelMerchno= key.channelMerchno;//渠道商户号
          }
      })
      // 立即还款
      $(".bankList").on("click","#customPlan",function(){
        customPlan($(this));
      })
      // 立即还款
      $(".bankList").on("click","#planDetails",function(){
        planDetails($(this));
      })
      // 充值
      $("#gotoRecharge").on("click",function(){
        window.location.href='index_recharge.html?loginName=' + loginName + '&aisle=' + channelCode + '&addRate=' + addRate + '&rmentMode=' + rmentMode + '&ruleCode=' + ruleCode + '&isReloadCard='+false+'&id='+id+''
      })
      // 提现
      $("#withdraw").on("click",function(){
        window.location.href='wallet_accountBalance.html?loginName=' + loginName + '';
      })
  })
}

/**
 * 获取信用卡列表
 */
function getList () {
    $(".loding").show(0);
    var data = {
      userName: baseFunction.encryption(loginName)
    };
    baseFunction.netRequest("quickCardBind", data, function (res) {
      $(".loding").hide(0);
       //解密
      if (res.code == 200) {
        var data = JSON.parse(JSON.parse(res.data).cards);
        if(data.length==0){ //没有绑定信用卡
          var html="<p style='font-size: 0.2rem;text-align: center'>暂没有绑定卡,请先去绑卡<p>";
          $(".bankList").html(html);
          return;   
        }
        for (var i = 0; i < data.length; i++) {
          data[i].endNO = (data[i].cardno).slice(-4);
          data[i].data = baseFunction.getData(data[i].billDate, data[i].rmentDate);
          for (var j = 0; j < bankCard.length; j++) {
            if ((data[i].bankName).indexOf(bankCard[j].bankName) != -1) {
              data[i].bg = "../static/img/" + bankCard[j].bankBackground + "@2x.png";
              data[i].bg2 = "../static/img/" + bankCard[j].bigLogo + ".png"
            }
          }
        }
        console.log(data);
        var html="";
        data.forEach(function(key){
            var status=key.planStatus==1?"未还款":"还款中";
            html+="<div class='item' >"
                +"<div class='li'>"
                +"<img src='"+key.bg+"'>"
                +"<div class='main'>"
                +"<div class='top'>"
                +"<img src='"+key.bg2+"'>"
                +"<div class='name'>"
                +"<text>"+key.bankName+" (<text>"+key.endNO+"</text>)</text>"
                +"<text>"+key.trueName+"</text>"
                +"</div>"
                +"<text class='fr'>"+status+"</text>"
                +"</div>"
                +"<div class='bottom'>"
                +"<div>"
                +"<text>额度</text>"
                +"<text>"+key.limitAmount+"</text>"
                +"</div> "
                +"<div class='fr'>"
                +"<text>账单日期："+key.data.billingDay+"</text>"
                +"<text>还款日期："+key.data.repaymentDate+"</text>"
                +"</div> "
                +"</div>"
                +"</div>"
                +"</div>"
                +"<!--操作  -->"
                +"<div class='operating'  >"
                +"<div  id='customPlan' data_planStatus='"+key.planStatus+"' data_planstatus='"+key.planstatus+"' data_merchno='"+key.merchno+"' data_cardno='"+key.cardno+"' data_id='"+key.id+"'  data_validType='"+key.validType+"'>"
                +"<img src='../static/img/quanming_haika_icon_haikuan@2x.png'>"
                +"<text>立即还款</text>"
                +"</div>"
                +"<div  id='gotoModifyCreditCard' data_id='"+key.id+"'>"
                +"<img src='../static/img/quanming_haika_icon_amend@2x.png'>"
                +"<text>修改资料</text>"
                +"</div>"
                +"<div  id='planDetails' data_planStatus='"+key.planStatus+"'>"
                +"<img src='../static/img/quanming_haika_icon_plan@2x.png'>"
                +"<text>计划详情</text>"               
                +"</div>"
                +"<div  id='untieTheCard' data_cardno='"+key.cardno+"' >"
                +"<img src='../static/img/quanming_haika_icon_untie@2x.png'>"
                +"<text>卡片解绑</text>"
                +"</div>"
                +"</div>"
                +"</div>"
        }); 
        $(".bankList").html(html);   

      } else {
        $(".prompt").html(res.message).show(0).delay(1000).hide(0);
      }
    })
}

/**
 * 查询渠道账户余额
 */
function getAisleInfo(){
  var data={
    userName: baseFunction.encryption(loginName),
    queryType:"6011",
    arg1: baseFunction.encryption(channelCode),
  };
  baseFunction.netRequest("param",data,function(res){
     //解密
    var data = JSON.parse(JSON.parse(res.data).result);
    if(data.length>0){ //有充值记录 就取第一条记录中的amount
      $(".balance .money").html(data[0].amount);
      accountBalance=data[0].amount;//账户余额
    }else{ //没有充值记录 默认为0
      $(".balance .money").html("0.00");
      accountBalance="0.00";//账户余额
    }
  })
}

/**
 * 解绑卡
 * @param {*} cardno 卡号
 */
function removeBankCard(cardno){
  $(".prompt").html("解绑中...").show(0);
  var data={
      userName:baseFunction.encryption(loginName),
      cardno:baseFunction.encryption(cardno)
  }
  baseFunction.netRequest("delQuickCard",data,function(res){
       //解密
      $(".prompt").html(res.message).show(0).delay(1000).hide(0);
      if (res.code==200){ //成功
          setTimeout(function() {
              window.location.href=window.location.href; 
          }, 1000);
      }
  })
}

/**
 * 还款计划
 */
function customPlan(obj){
  $(".loding").show(0);
  var planStatus = obj.attr("data_planstatus"); //还款计划状态 1 未还款  2 3还款中
  var merchno = obj.attr("data_merchno"); //商户号
  var cardno = obj.attr("data_cardno"); //银行卡号
  var id = obj.attr("data_id"); //银行列表中当前银行卡的标识
  var validType = obj.attr("data_validType") ; 
  if (planStatus == 1){ //未还款
    if (validType == 2 || validType == 3) {
      //1验证是否绑卡
      var data = {
        userName: baseFunction.encryption(loginName),
        channelCode: channelCode,
        channelMerchno: channelMerchno,
        cardno: baseFunction.encryption(cardno),
      };
      baseFunction.netRequest("validCardBind", data, function (res) {
        $(".loding").hide(0);
        
        if (res.code == 200) { //请求成功
          var status = JSON.parse(res.data).status;
          if (status == 1) { // 已绑定
            window.location.href='index_customPlan.html?loginName=' + loginName + '&aisle=' + channelCode + '&id=' + id + '&ruleCode=' + ruleCode + '&accountBalance' + accountBalance + '&addRate=' + addRate + ' &rmentMode=' + rmentMode + ' ';
          } else if (validType == 3) {
            //2跳转银联在线验证接口
            var data = {
              userName: baseFunction.encryption(loginName),
              interType: 2,
              transType: 2,
              channelCode: that.data.aisle,
              merchno: baseFunction.encryption(merchno),
              cardno: baseFunction.encryption(cardno),
              channelMerchno: channelMerchno,
            };
            baseFunction.netRequest("bindApplyAys", data, function (res) { //进入银联办卡
              
              $(".prompt").html(res.message).show(0).delay(1000).hide(0);
              if(res.code==200){
                var url = JSON.parse(res.data).barCode;
                setTimeout(function(){
                  window.location.href=url;
                },1000);
              }
              
            })
          } else { //提示充值绑卡
            $(".loding").hide(0);
            $(".mask .tip").html("渠道限制需要充值绑卡");
            $(".mask").show(0);
            var cardno=$(this).attr("data_cardno");
            var btn=document.querySelector(".mask .btn");
            var mask=document.querySelector(".mask");
            btn.onclick=function(){ //确定 跳转充值
              window.location.href='index_recharge.html?loginName=' + loginName + '&aisle=' + channelCode + '&addRate=' + addRate + '&rmentMode=' + rmentMode + '&ruleCode=' + ruleCode + '&isReloadCard='+true+'&id='+id+'';
            }
          }
        }
      })
    } else {
      $(".loding").hide(0);
      console.log(addRate.toString());
      window.location.href='index_customPlan.html?loginName=' + loginName + '&aisle=' + channelCode + '&id=' + id + '&ruleCode=' + ruleCode + '&accountBalance=' + accountBalance+ '&rmentMode=' + rmentMode+ '&addRate='+addRate+'';
    }
    
  }else{ //还款中 计划详情页
    $(".loding").hide(0);
    $(".mask .tip").html("您已定制还款了计划，是否查看还款计划");
    $(".mask").show(0);
    var cardno=$(this).attr("data_cardno");
    var btn=document.querySelector(".mask .btn");
    var mask=document.querySelector(".mask");
    btn.onclick=function(){ //确定 跳转去看计划详情
      window.location.href="index_plannedOrder.html?loginName=" + loginName + "&aisle=" + channelCode + "&id=" + id + "";
    }
  }
}


/**
 * 计划详情
 */
function planDetails(obj){
  var planStatus = obj.attr("data_planstatus"); //还款计划状态 1 未还款  2 3还款中
  if (planStatus==1){ //未定制还款计划
    $(".mask .tip").html("当前信用卡暂未定制还款计划。是否现在定制计划？");
    $(".mask").show(0);
    var cardno=$(this).attr("data_cardno");
    var btn=document.querySelector(".mask .btn");
    var mask=document.querySelector(".mask");
    btn.onclick=function(){ //确定 跳转去看计划详情
      var obj=$(".bankList #customPlan");
      customPlan(obj);
    } 
  }else{ 
    window.location.href='index_plannedOrder.html?loginName=' + loginName + '&aisle=' + channelCode + '';
  }
}